package com.dp.appi.controller;

import com.dp.appi.entity.UserEntity;
import com.dp.appi.service.ConsumerService;
import com.dp.appi.util.ExceptionUtil;
import com.dp.appi.util.PasswordUtil;
import com.dp.appi.util.SYS;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * Created by xiaoji on 2016/4/20.
 * 用户
 */
@Controller
@RequestMapping(value = "/consumerControl")
public class ConsumerControl {
    private static Logger log = LoggerFactory.getLogger(ConsumerControl.class);
    @Autowired
    private ConsumerService consumerService;

    /**
     * 获取指定用户id
     *
     * @param modelMap
     * @param param    openIdent
     */
    @RequestMapping(value = "/getConsumerId", method = RequestMethod.GET)
    public void getConsumerId(ModelMap modelMap, @RequestParam Map<String, String> param) {
        try {
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);
            modelMap.addAttribute("consumer_id", map.get("id"));
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 获取用户基本信息
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/consumerInfo", method = RequestMethod.GET)
    public void consumerInfo(ModelMap modelMap, @RequestParam Map<String, String> param) {
        try {
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);
            modelMap.addAttribute("user_id", map.get("id"));
            modelMap.addAttribute("username", map.get("username"));
            modelMap.addAttribute("mobilePhone", map.get("mobilePhone"));
            modelMap.addAttribute("realname", map.get("realname"));
            modelMap.addAttribute("avatar", map.get("avatar"));
            modelMap.addAttribute("email", map.get("email"));
            modelMap.addAttribute("qq", map.get("qq"));
            modelMap.addAttribute("wx", map.get("wx"));

            modelMap.addAttribute("is_vip", map.get("isVip"));
            modelMap.addAttribute("vip_start", map.get("vipStart"));
            modelMap.addAttribute("vip_end", map.get("vipEnd"));
            modelMap.addAttribute("current_date", map.get("currentDate"));
            modelMap.addAttribute("openIdent",openIdent);
            if (map.get("birthday") != null) {
                Date birthday = (Date) (map.get("birthday"));
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                modelMap.addAttribute("birthday", simpleDateFormat.format(birthday));
            }
            modelMap.addAttribute("sex", map.get("sex"));
            modelMap.addAttribute("usertype", map.get("usertype"));
            modelMap.addAttribute("company", map.get("company"));
            //组织机构
            Map<String, Object> partMap = consumerService.getTSpartByUserId(String.valueOf(map.get("id")));
            if (partMap != null) {
                modelMap.addAttribute("org_id", partMap.get("org_id"));
                modelMap.addAttribute("org_type", partMap.get("org_type"));
            }

        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 修改用户基本信息
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/upConsumerInfo", method = RequestMethod.POST)
    public void upConsumerInfo(ModelMap modelMap, @RequestParam Map<String, String> param) {
        try {
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);
            param.put("id", String.valueOf(map.get("id")));
            consumerService.updateUser(param);
            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

}
